package com.tongzhan.service.impl;

import com.tongzhan.base.PageInfo;
import com.tongzhan.base.Return;
import com.tongzhan.data.vo.*;
import com.tongzhan.entity.religion.CompanyType;
import com.tongzhan.entity.religion.ExemplaryCompany;
import com.tongzhan.entity.religion.Representatives;
import com.tongzhan.repository.religion.CompanyTypeRepository;
import com.tongzhan.repository.religion.ExemplaryCompanyRepository;
import com.tongzhan.repository.religion.RepresentativesRepository;
import com.tongzhan.service.GraceService;
import com.tongzhan.utils.SnowFlakeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GraceServiceImpl implements GraceService {

    @Autowired
    private RepresentativesRepository representativesRepository;

    @Autowired
    private ExemplaryCompanyRepository exemplaryCompanyRepository;

    @Autowired
    private CompanyTypeRepository companyTypeRepository;

    @Override
    public Return newRepresentatives(Representatives representatives) {
        //生成代表人士编码
        SnowFlakeUtils snow = new SnowFlakeUtils(4, 10);
        representatives.setRepresentCode(String.valueOf(snow.nextId()));
        representatives.setCreateTime(LocalDateTime.now());
        representativesRepository.insert(representatives);
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getRepresentativesList(Integer page, Integer size) {
        //查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "CreateTime"));
        Page<Representatives> representatives = representativesRepository.findPage(query, PageRequest.of(page - 1, size));
        //代表人士列表
        List<Representatives> representativesList = representatives.getContent();
        //VO
        List<RepresentativesVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(representativesList)) {
            for (Representatives represent : representativesList) {
                RepresentativesVO vo = new RepresentativesVO();
                vo.setRepresentCode(represent.getRepresentCode());
                vo.setName(represent.getName());
                vo.setSex(represent.getSex());
                vo.setAge(represent.getAge());
                vo.setCompany(represent.getCompany());
                vos.add(vo);
            }
        }
        //分页信息
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setSize(vos.size());
        pageInfo.setTotal(representatives.getTotalElements());
        //封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("representativesList", vos);
        map.put("pageInfo", pageInfo);
        return new Return(map);
    }

    @Override
    public Return representativesDetails(String representCode) {
        Representatives representatives = representativesRepository.findByPrimaryKey(representCode);
        //VO
        RepresentativesDetailsVO detailsVO = new RepresentativesDetailsVO();
        if (representatives != null) {
            detailsVO.setRepresentCode(representatives.getRepresentCode());
            detailsVO.setName(representatives.getName());
            detailsVO.setAge(representatives.getAge());
            detailsVO.setSex(representatives.getSex());
            detailsVO.setCompany(representatives.getCompany());
            detailsVO.setIntroduction(representatives.getIntroduction());
            detailsVO.setGrace(representatives.getGrace());
            //图片
            String picture = representatives.getPicture();
            List<PicVO> picList = new ArrayList<>();
            if (StringUtils.isNotEmpty(picture)) {
                String[] files = picture.split(",");
                for (String file : files) {
                    PicVO picVO = new PicVO();
                    picVO.setPath(file);
                    picList.add(picVO);
                }
            }
            detailsVO.setPicList(picList);
        }
        return new Return(detailsVO);
    }

    @Override
    public Return newExemplaryCompany(ExemplaryCompany exemplaryCompany) {
        //公司类型
        CompanyType companyType = companyTypeRepository.findByPrimaryKey(exemplaryCompany.getTypeCode());
        if (companyType != null) {
            exemplaryCompany.setTypeName(companyType.getTypeName());
        }
        //生成编码
        SnowFlakeUtils snow = new SnowFlakeUtils(4, 11);
        exemplaryCompany.setCompanyCode(String.valueOf(snow.nextId()));
        exemplaryCompany.setCreateTime(LocalDateTime.now());
        exemplaryCompanyRepository.insert(exemplaryCompany);
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getExemplaryCompanyList(Integer page, Integer size) {
        //查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "CreateTime"));
        Page<ExemplaryCompany> exemplaryCompanies = exemplaryCompanyRepository.findPage(query, PageRequest.of(page - 1, size));
        //列表
        List<ExemplaryCompany> companyList = exemplaryCompanies.getContent();
        //VO
        List<ExemplaryCompanyVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(companyList)) {
            for (ExemplaryCompany company : companyList) {
                ExemplaryCompanyVO vo = new ExemplaryCompanyVO();
                vo.setCompanyCode(company.getCompanyCode());
                vo.setCompanyName(company.getCompanyName());
                vo.setTypeName(company.getTypeName());
                vos.add(vo);
            }
        }
        //分页信息
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setSize(vos.size());
        pageInfo.setTotal(exemplaryCompanies.getTotalElements());
        //封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("exemplaryCompanyList", vos);
        map.put("pageInfo", pageInfo);
        return new Return(map);
    }

    @Override
    public Return exemplaryCompanyDetails(String companyCode) {
        ExemplaryCompany exemplaryCompany = exemplaryCompanyRepository.findByPrimaryKey(companyCode);
        //VO
        ExemplaryCompanyDetailsVO detailsVO = new ExemplaryCompanyDetailsVO();
        if (exemplaryCompany != null) {
            detailsVO.setCompanyCode(exemplaryCompany.getCompanyCode());
            detailsVO.setCompanyName(exemplaryCompany.getCompanyName());
            detailsVO.setAddress(exemplaryCompany.getAddress());
            detailsVO.setBuildTime(exemplaryCompany.getBuildTime());
            detailsVO.setTypeName(exemplaryCompany.getTypeName());
            detailsVO.setNumber(exemplaryCompany.getNumber());
            detailsVO.setGrace(exemplaryCompany.getGrace());
            //图片
            String picture = exemplaryCompany.getPicture();
            List<PicVO> picList = new ArrayList<>();
            if (StringUtils.isNotEmpty(picture)) {
                String[] files = picture.split(",");
                for (String file : files) {
                    PicVO picVO = new PicVO();
                    picVO.setPath(file);
                    picList.add(picVO);
                }
            }
            detailsVO.setPicList(picList);
        }
        return new Return(detailsVO);
    }

    @Override
    public Return editCompanyType(String typeCode, String typeName) {
        if (StringUtils.isEmpty(typeCode)) {
            //新增
            CompanyType companyType = new CompanyType();
            SnowFlakeUtils snow = new SnowFlakeUtils(4, 12);
            companyType.setTypeCode(String.valueOf(snow.nextId()));
            companyType.setTypeName(typeName);
            companyType.setCreateTime(LocalDateTime.now());
            companyTypeRepository.insert(companyType);
        } else {
            //编辑
            CompanyType companyType = companyTypeRepository.findByPrimaryKey(typeCode);
            if (companyType != null) {
                companyType.setTypeName(typeName);
                companyType.setUpdateTime(LocalDateTime.now());
            }
            companyTypeRepository.updateByPrimaryKey(companyType);
        }
        return new Return(Return.SUCCESS, "操作成功");
    }

    @Override
    public Return getCompanyTypeList() {
        List<CompanyType> companyTypeList = companyTypeRepository.findAll();
        //VO
        List<CompanyTypeVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(companyTypeList)) {
            for (CompanyType companyType : companyTypeList) {
                CompanyTypeVO vo = new CompanyTypeVO();
                vo.setTypeCode(companyType.getTypeCode());
                vo.setTypeName(companyType.getTypeName());
                vos.add(vo);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("companyTypeList", vos);
        return new Return(map);
    }

    @Override
    public Return deleteCompanyType(String typeCode) {
        companyTypeRepository.delete(new Query().addCriteria(Criteria.where("TypeCode").is(typeCode)));
        return new Return(Return.SUCCESS, "操作成功");
    }
}
